// Copyright 2022 marcello<volibearw@gmail.com>. All rights reserved.

package main

// generateModelStructPart 生成struct部分
func generateModelStructPart(param *CLIParam, tableName string, columns []Columns) string {

	var structStr string
	if len(columns) == 0 {
		// 没有字段的情况
		structStr += "type " + toCamelString(tableName) + " struct{\n"
		structStr += "}\n"
		return structStr
	}

	structStr += "// " + columns[0].TableNameCame + " 表\n"
	structStr += "type " + columns[0].TableNameCame + " struct{\n"
	if param.ModelUseGorm && param.ModelIncludeGormModel {
		structStr += "    gorm.Model\n"
	}

	// 组装字段部分
	var field string
	for _, val := range columns {

		if param.ModelUseGorm && param.ModelIncludeGormModel {
			// 如果使用了 gorm.Model 就必须跳过这几个字段
			if isGormDefaultModelField(val.ColumnName) {
				continue
			}
		}

		field += "    " + val.ColumnNameCame + " " + val.ColumnGoDataType
		if param.ModelUseGorm {
			field += " `gorm:\"column:" + val.ColumnName + "\"`\n"
		} else {
			field += "\n"
		}
	}
	structStr += field + "}\n\n"

	// 生成实例化 New 部分
	if param.ModelUseGorm {
		structStr += "// TableName 设置表名\n"
		structStr += "func (*" + columns[0].TableNameCame + ") TableName() string {\n"
		structStr += "    return \"" + tableName + "\"\n"
		structStr += "}\n\n"
	}
	return structStr
}
